<template>
  <div>
    <el-card class="main-card">
      <!--
                v-if 先判断，后隐藏没有直接删除document 造成性能问题，对标签元素进行删除和展示
                 v-show 只是加了display属性
                 应用场景 ： 经常对元素属性进行展示的用 v-show， 反正用v-if
             -->
      <div v-show="!open">
        <el-row>
          <el-col :span="9">
            <el-input
              placeholder="请输入登录名/昵称/性别/邮箱"
              clearable
              v-model="queryInfo.queryString"
              @clear="findPage"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="findPage"
              ></el-button>
            </el-input>
          </el-col>
          <el-col :span="2">
            <el-button
              @click="insert"
              style="margin-left: 10px"
              v-has-permission="['USER_INSERT']"
              type="primary"
              >添加用户</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="tableList"
          stripe
          border
          highlight-current-row
          v-loading="loading"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          max-height="450"
        >
          <el-table-column type="index" fixed="left" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="name" label="用户登录名" width="100">
          </el-table-column>
          <el-table-column prop="nickName" label="昵称"> </el-table-column>
          <el-table-column label="头像" width="120" prop="avatar">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="
                  scope.row.avatar.startsWith('https')
                    ? scope.row.avatar
                    : $qiniu + scope.row.avatar
                "
                :preview-src-list="preview(scope.row.avatar)"
              >
              </el-image>
            </template>
          </el-table-column>
          <el-table-column prop="sex" label="性别" :formatter="formatSex">
          </el-table-column>
          <el-table-column prop="openId" label="小程序Id"> </el-table-column>
          <el-table-column prop="email" label="邮箱" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="address" label="地址" show-overflow-tooltip>
          </el-table-column>
          <el-table-column
            label="超级管理员"
            v-has-role="['SUPER_ADMIN']"
            width="100px"
            v-has-permission="['SUPER_ADMIN']"
          >
            <template slot-scope="scope">
              <el-switch
                @change="updateStatus(scope.row)"
                v-model="scope.row.admin"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="启用状态" v-has-permission="['USER_UPDATE']">
            <template slot-scope="scope">
              <el-switch
                @change="updateStatus(scope.row)"
                v-model="scope.row.status"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="角色标签" fixed="left">
            <template slot-scope="scope">
              <el-tag
                size="mini"
                v-for="(item, index) in scope.row.roles"
                :key="index"
              >
                {{ item.label }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template slot-scope="scope">
              <el-button
                @click="updateById(scope.row)"
                type="primary"
                v-has-permission="['USER_UPDATE']"
                icon="el-icon-edit"
                size="small"
              ></el-button>
              <el-button
                type="danger"
                @click="deleteById(scope.row)"
                v-has-permission="['USER_DELETE']"
                icon="el-icon-delete"
                size="small"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handlePageSize"
          @current-change="handlePageNumber"
          background
          :current-page="queryInfo.pageNumber"
          :page-sizes="[8, 16, 32, 64, 128]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      <handle v-show="open" :title="title" @cancel="cancel" :form="form" />
    </el-card>
  </div>
</template>

<script>
import handle from "./handle.vue";
export default {
  /** 注册组件 */
  components: {
    handle,
  },
  data() {
    return {
      // 分页查询条件
      queryInfo: {
        // 页码
        pageNumber: 1,
        // 页数
        pageSize: 8,
        // 关键字
        queryString: null,
        // 表格数据
      },
      tableList: [],
      // 总记录数
      total: 0,
      // 表单标题
      title: null,
      // 是否打开对话框
      open: false,
      // 表格加载
      loading: false,
      // 表单数据
      form: {},
      // 角色列表
      roles: [],
    };
  },
  created() {
    this.findPage();
  },
  methods: {
    /** 分页查询 */
    findPage() {
      this.loading = true;
      this.$ajax.post("/sysUser/findPage", this.queryInfo).then((res) => {
        console.log("用户信息", res);
        this.loading = false;
        this.tableList = res.data.rows;
        this.total = res.data.total;
      });
    },
    /** 页面大小改变事件 */
    handlePageSize(newPageSize) {
      this.queryInfo.pageSize = newPageSize;
      this.findPage();
    },
    /** 页面改变事件 */
    handlePageNumber(newPageNumber) {
      // 将分页参数新数据传递给pageNumber(分页参数)
      this.queryInfo.pageNumber = newPageNumber;
      this.findPage();
    },
    /** 修改用户信息 */
    insert() {
      this.open = true;
      this.form = {};
      this.title = "添加用户";
    },
    /** 修改用户信息 */
    updateById(row) {
      console.log("体检修改", row);
      this.form = row;
      this.title = "修改用户";
      this.open = true;
    },
    /** 删除用户信息 */
    deleteById(row) {
      if (row.admin) {
        return this.$message.warning("管理员角色不可删除！");
      }
      this.$confirm(
        "您的操作将永久删除编号为{" + row.id + "}的数据, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 调后台退出接口
          this.$ajax.delete(`/sysUser/delete/${id}`).then((res) => {
            this.$message.success(res.data.message);
            this.queryInfo.pageNumber = 1;
            this.findPage();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    /** 改变用户状态 */
    updateStatus(row) {
      this.$ajax.put("/sysUser/update/", row).then((res) => {
        this.$message.success("状态更新成功!");
      });
    },
    /** 图片预览*/
    preview(src) {
      let arr = [];
      if (src) {
        if (!src.startsWith("https")) {
          src = this.$qiniu + src;
        }
      }
      // arr.push(src.startsWith('https')?src:this.$qiniu+src);
      arr.push(src);
      return arr;
    },
    /** 格式化性别 */
    formatSex(row) {
      if (row.sex === false) {
        return "男";
      } else if (row.sex === true) {
        return "女";
      } else {
        return "未知";
      }
    },
    /** 用户点击返回 */
    cancel() {
      this.open = false;
      this.findPage();
    },
  },
};
</script>

<style scoped></style>
